#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
#define int long long
const int INF = 0x3f3f3f3f3f3f3f3f;
 
signed main() // 01背包+同余
{
	int n = 0, k = 0;
	cin >> n >> k;
	vector<int> arr(n + 1);
	for (int i = 1; i <= n; ++i)
	{
		cin >> arr[i];
	}
    vector<vector<long long>> dp(N + 1, vector<long long>(N + 1, -INF));
	// dp[i][j]表示从1到i中选，总和 %k 等于j时，最大和是多少
    dp[0][0] = 0;
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 0; j < k; ++j)
		{
            dp[i][j] = max(dp[i - 1][j], dp[i - 1][(j - arr[i] % k + k) % k] + arr[i]);
		}
	}
	cout << (dp[n][0] <= 0 ? -1 : dp[n][0]) << endl;
	return 0;
}